package com.lingx.web.api.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.lingx.service.LingxService;
import com.lingx.web.api.IApi;
@Component
public class Api9026 extends AbstractDevApi implements IApi{
	@Resource
	private ApplicationContext spring;
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private LingxService lingxService;
	@Override
	public int getApiCode() {
		return 9026;
	}
	@Override
	public String getApiName() {
		return "获取接口树";
	}
	@Override
	public Map<String, Object> api(Map<String, Object> params) {
		Map<String,Object> ret=IApi.getRetMap(1, "操作成功");
		String fid=IApi.getParamString(params, "fid", "");
		String roleid=IApi.getParamString(params, "role_id", "");
		List<String> checkedKeys=new ArrayList<>();
		List<Map<String,Object>> list=treeMenu(fid,roleid,checkedKeys,this.jdbcTemplate);
		ret.put("data", list);
		ret.put("checkedKeys", checkedKeys);
		return ret;
	}
	public List<Map<String,Object>> treeMenu(String fid,String roleid,List<String> checkedKeys,JdbcTemplate jdbc){
		List<Map<String,Object>> list=getGroupList(fid);//jdbc.queryForList("select id,name text from tlingx_menu where fid=? and status='1' order by orderindex asc",fid);
		boolean b=false;
		for(Map<String,Object> m:list){
			b= jdbc.queryForObject("select count(*) from tlingx_role_api where role_id=? and apicode=?", Integer.class,roleid,m.get("id"))>0;
			if(b)checkedKeys.add(m.get("id").toString());
			m.put("checked",b);
			if("0".equals(fid))
			m.put("state","close");
			List<Map<String,Object>> listsub=treeMenu(m.get("id").toString(),roleid,checkedKeys,jdbc);
			if(listsub.size()>0){
				m.put("children",listsub);
				m.put("checked",false);
				m.put("leaf", false);
				if(b)checkedKeys.remove(m.get("id").toString());
			}else {
				m.put("leaf", true);
				
			}
		}
		return list;
	}
	private List<Map<String,Object>> getGroupList(String fid){

		Map<String,IApi> listApi=this.spring.getBeansOfType(IApi.class);
		List<Map<String,Object>> list=new ArrayList<>();
		Map<String,Object> map;
		if("0".equals(fid)) {
		Set<String> sets=new HashSet<>();
		for(IApi api:listApi.values()) {
			sets.add(api.getGroupName());
		}
		for(String str:sets) {
			map=new HashMap<>();
			map.put("text", str);
			map.put("id", str);
			list.add(map);
		}
		
		}else {
			for(IApi api:listApi.values()) {
				if(fid.equals(api.getGroupName())) {
					map=new HashMap<>();
					map.put("text", api.getApiCode()+" - "+api.getApiName());
					map.put("id", api.getApiCode());
					list.add(map);
				}
			}
		}
		
		Collections.sort(list, new Comparator<Map<String,Object>>() {
			@Override
			public int compare(Map<String,Object> o1, Map<String,Object> o2) {
				Map<String,Object> m1=o1;
				Map<String,Object> m2=o2;
				return (m1.get("id").toString().compareTo(m2.get("id").toString())) ;
			}});
		return list;
	}

}
